import urllib.request
from lxml import etree


def data_ding(i1):
    if i1 == 1:
        url = "https://sc.chinaz.com/tupian/dadanrenti.html"
    else:
        url = "https://sc.chinaz.com/tupian/dadanrenti_" + str(i1) + ".html"

    headers = {
        "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/94.0.4606.71 Safari/537.36 Edg/94.0.992.38"
    }
    resquest = urllib.request.Request(url=url, headers=headers)
    # print(resquest)
    return resquest


# //div[@id="container"]/div/div/a/img/@src
def data_cont(i2):
    response = urllib.request.urlopen(i2)
    content = response.read().decode("utf-8")
    # print(content)
    return content


def down_load(i3):
    tree = etree.HTML(i3)
    alt1 = tree.xpath("//div[@id='container']/div/div/a/img/@alt")
    src1 = tree.xpath("//div[@id='container']/div/div/a/img/@src2")
    # print(len(conttt))
    for j in range(len(alt1)):
        name_down = alt1[j]
        src_down = src1[j]
        url_down = "https:" + src_down
        urllib.request.urlretrieve(url=url_down, filename="./load_img/" + name_down + ".jpg")
if __name__ == '__main__':
    data1 = int(input("起始页码"))
    data2 = int(input("结束页码"))
    for ii in range(data1, data2 + 1):
        res = data_ding(ii)
        cont = data_cont(res)
        down_load(cont)
